package com.martin.ads.omoshiroilib.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseActivity;
import com.imageim.yimirror.bean.UploadOriginalMemPhotoResultBean;
import com.imageim.yimirror.common.ui.MyFilterImageView;
import com.martin.ads.omoshiroilib.filter.helper.FilterType;
import com.martin.ads.omoshiroilib.flyu.hardcode.DemoConstants;
import com.martin.ads.omoshiroilib.imgeditor.gl.GLWrapper;
import com.martin.ads.omoshiroilib.util.BitmapUtils;
import com.martin.ads.omoshiroilib.util.FileUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 相片滤镜
 */
public class AddFilterActivity extends BaseActivity {
    private RecyclerView filterListView;
    private MyFilterImageView glRootView;
    private GLWrapper glWrapper;
    private UploadOriginalMemPhotoResultBean originalPhotoResultBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_edit);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        getWindow().setAttributes(params);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initBundle() {
        originalPhotoResultBean = (UploadOriginalMemPhotoResultBean) getIntent().getSerializableExtra("originalPhotoResultBean");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit;
    }

    @Override
    protected void initView() {
        setFilterList();
        setUpImage();
        glRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                glWrapper.addRunnable(new Runnable() {
                    @Override
                    public void run() {
                        // Generate picture from gl render.
                        BitmapUtils.sendImage(bitmap.getWidth(), bitmap.getHeight(), AddFilterActivity.this, new FileUtils.FileSavedCallback() {
                            @Override
                            public void onFileSaved(String filePath) {
                                //                                GlideUtils.display(iv, filePath);
                                //                                Bundle data = new Bundle();
                                //                                data.putString("photoPath", filePath);
                                //                                data.putSerializable("originalPhotoResultBean", originalPhotoResultBean);
                                //                                SceneManager.toScene(mContext, SelectPhotoFrameActivity.class, data);
                                finish();
                            }
                        });
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {
    }

    private Bitmap bitmap;

    private void setUpImage() {
        String path = getIntent().getStringExtra(DemoConstants.IMAGE_PATH);
        bitmap = BitmapUtils.loadBitmapFromFile(path);
        //        bitmap.recycle();
        //        bitmap = Bitmap.createScaledBitmap(bitmap, 600, 1000, true);
        glRootView.setAspectRatio(bitmap.getHeight(), bitmap.getWidth());
        //        try {
        //            boolean isok = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(path));
        glWrapper.setFilePath(path);
        //        } catch (FileNotFoundException e) {
        //            e.printStackTrace();
        //        }
    }

    private void setFilterList() {
        glRootView = findViewById(R.id.camera_view);
        glWrapper = GLWrapper.newInstance()
                .setGlImageView(glRootView)
                .setContext(this)
                .init();

        FileUtils.upZipFile(this, "filter/thumbs/thumbs.zip", getFilesDir().getAbsolutePath());

        filterListView = findViewById(R.id.filter_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        filterListView.setLayoutManager(linearLayoutManager);

        List<FilterType> filterTypeList = new ArrayList<>();
        for (int i = 0; i < FilterType.values().length; i++) {
            filterTypeList.add(FilterType.values()[i]);
            if (i == 0) {
                filterTypeList.add(FilterType.NONE);
            }
        }
        //        FilterAdapter filterAdapter = new FilterAdapter(this,filterTypeList);
        //        filterListView.setAdapter(filterAdapter);
        //        filterAdapter.setOnFilterChangeListener(new FilterAdapter.OnFilterChangeListener() {
        //            @Override
        //            public void onFilterChanged(FilterType filterType) {
        //                glWrapper.switchLastFilterOfCustomizedFilters(filterType);
        //            }
        //        });
    }

}
